<template>
  <div class="app-container payment-page" v-loading="loading">
    <div class="order-info" v-if="orderInfo.id">
      <el-card class="box-card order-card">
        <template #header>
          <div class="card-header">
            <span class="header-title">订单信息</span>
          </div>
        </template>
        <el-descriptions :column="2" border class="custom-descriptions">
          <el-descriptions-item label="订单编号">
            <span class="order-sn">{{ orderInfo.orderSn }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="订单金额">
            <span class="amount">￥{{ orderInfo.totalAmount }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="支付状态">
            <el-tag :type="orderInfo.status === 0 ? 'warning' : 'success'" class="status-tag">
              {{ orderInfo.status === 0 ? '待支付' : '已支付' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="创建时间">
            <span class="time">{{ orderInfo.createTime }}</span>
          </el-descriptions-item>
        </el-descriptions>
      </el-card>

      <div class="pay-methods" v-if="orderInfo.status === 0">
        <el-card class="box-card payment-card">
          <template #header>
            <div class="card-header">
              <span class="header-title">选择支付方式</span>
            </div>
          </template>
          <div class="payment-options">
            <el-radio-group v-model="payMethod" class="custom-radio-group">
              <el-radio :label="1" class="payment-radio">
                <div class="payment-method-item">
                  <img src="@/assets/images/alipay.svg" alt="支付宝" class="payment-icon">
                  <span class="payment-name">支付宝</span>
                </div>
              </el-radio>
              <el-radio :label="2" class="payment-radio">
                <div class="payment-method-item">
                  <img src="@/assets/images/wechat.svg" alt="微信支付" class="payment-icon">
                  <span class="payment-name">微信支付</span>
                </div>
              </el-radio>
            </el-radio-group>
          </div>
          <div class="pay-actions">
            <el-button type="primary" size="large" @click="handlePay" :loading="paying">
              立即支付
            </el-button>
            <el-button type="default" size="large" @click="handlePayLater">
              稍后支付
            </el-button>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { getOrder, payOrder } from '@/api/order'
import { ElMessage } from 'element-plus'

const route = useRoute()
const router = useRouter()
const loading = ref(false)
const orderInfo = ref({})
const payMethod = ref(null)

// 获取订单详情
const getOrderDetails = async () => {
  const orderSn = route.params.orderSn
  if (!orderSn) {
    ElMessage.error('订单编号不能为空')
    return
  }

  try {
    loading.value = true
    const response = await getOrder(orderSn)
    if (response.code === 200 && response.data) {
      orderInfo.value = response.data
    } else {
      ElMessage.error(response.msg || '获取订单信息失败')
    }
  } catch (error) {
    console.error('获取订单信息失败:', error)
    ElMessage.error('获取订单信息失败')
  } finally {
    loading.value = false
  }
}

// 处理支付
const handlePay = async () => {
  if (!payMethod.value) {
    ElMessage.warning('请选择支付方式')
    return
  }
  
  try {
    loading.value = true
    const response = await payOrder(orderInfo.value.orderSn, payMethod.value)
    if (response.code === 200) {
      ElMessage.success('支付成功')
      router.push(`/order/result?orderSn=${orderInfo.value.orderSn}&status=success`)
    } else {
      ElMessage.error(response.msg || '支付失败')
    }
  } catch (error) {
    console.error('支付失败:', error)
    ElMessage.error('支付失败')
  } finally {
    loading.value = false
  }
}

// 处理稍后支付
const handlePayLater = () => {
  ElMessage.info('您可以在订单列表中继续支付')
  router.push('/order/list')
}

onMounted(() => {
  getOrderDetails()
})
</script>

<style lang="scss" scoped>
.payment-page {
  min-height: 100vh;
  padding: 40px 20px;
  background: linear-gradient(135deg, #f6f8fb 0%, #f1f4f9 100%);
}

.order-info {
  max-width: 800px;
  margin: 0 auto;
}

.box-card {
  margin-bottom: 24px;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  border: none;
  overflow: hidden;
  transition: all 0.3s ease;
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  }
}

.order-card {
  background: linear-gradient(145deg, #ffffff, #f8f9fa);
}

.payment-card {
  background: #fff;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: linear-gradient(145deg, #ffffff, #f8f9fa);
  border-bottom: 1px solid #eef0f5;
  padding: 20px 24px;

  .header-title {
    font-size: 18px;
    font-weight: 600;
    color: #2c3e50;
    position: relative;
    padding-left: 12px;
    
    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      width: 4px;
      height: 18px;
      background: linear-gradient(45deg, #8B4513, #D2691E);
      border-radius: 2px;
    }
  }
}

.custom-descriptions {
  padding: 20px;
  
  :deep(.el-descriptions__label) {
    color: #606266;
    font-weight: 500;
    background-color: #f8f9fa;
  }
  
  :deep(.el-descriptions__content) {
    padding: 12px 16px;
  }
}

.order-sn {
  color: #2c3e50;
  font-family: monospace;
  font-weight: 500;
}

.amount {
  color: #f56c6c;
  font-size: 20px;
  font-weight: 600;
}

.time {
  color: #606266;
}

.status-tag {
  font-weight: 500;
  padding: 6px 12px;
  border-radius: 4px;
}

.payment-options {
  padding: 20px 0;
}

.custom-radio-group {
  display: flex;
  justify-content: center;
  gap: 40px;
  width: 100%;
  
  :deep(.el-radio) {
    margin-right: 0;
    height: auto;
  }
}

.payment-radio {
  .payment-method-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20px;
    border-radius: 12px;
    background: linear-gradient(145deg, #ffffff, #f8f9fa);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
    transition: all 0.3s ease;
    cursor: pointer;
    
    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 8px 16px rgba(0, 0, 0, 0.08);
    }
  }
  
  .payment-icon {
    width: 48px;
    height: 48px;
    margin-bottom: 12px;
    transition: transform 0.3s ease;
  }
  
  .payment-name {
    font-size: 14px;
    color: #606266;
    margin-top: 8px;
  }
  
  &:deep(.el-radio__input.is-checked + .el-radio__label) {
    color: #8B4513;
  }
  
  &:deep(.el-radio__input.is-checked .el-radio__inner) {
    background-color: #8B4513;
    border-color: #8B4513;
  }
}

.pay-actions {
  text-align: center;
  padding: 32px 0 16px;

  .el-button {
    margin: 0 12px;
    min-width: 140px;
    
    &--primary {
      background: linear-gradient(45deg, #8B4513, #D2691E);
      border: none;
      
      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 24px rgba(139, 69, 19, 0.25);
      }
    }
    
    &--default {
      border: 1px solid #DCDFE6;
      
      &:hover {
        border-color: #8B4513;
        color: #8B4513;
      }
    }
  }
}

// 响应式布局
@media screen and (max-width: 768px) {
  .payment-page {
    padding: 20px 16px;
  }
  
  .custom-radio-group {
    flex-direction: column;
    gap: 20px;
  }
  
  .payment-radio {
    .payment-method-item {
      padding: 16px;
    }
    
    .payment-icon {
      width: 40px;
      height: 40px;
    }
  }
  
  .pay-actions {
    .el-button {
      width: 100%;
      min-width: unset;
    }
  }
  
  .custom-descriptions {
    :deep(.el-descriptions__body) {
      display: flex;
      flex-direction: column;
      
      .el-descriptions__table {
        display: block;
        
        .el-descriptions__cell {
          display: block;
          width: 100%;
          
          &.is-bordered-content {
            width: 100%;
          }
        }
      }
    }
  }
}
</style>